package com.chainyoung.common.base;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.chainyoung.common.R;

import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.IView;
import com.chainyoung.common.view.kprogresshud.KProgressHUD;

import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import es.dmoral.toasty.Toasty;
import io.reactivex.subscribers.ResourceSubscriber;
import retrofit2.HttpException;

public abstract class CommonSubscriber<T> extends ResourceSubscriber<T> {

    private Context mContext;
    private IView mView;
    private String mErrorMsg;
    private boolean isShowErrorState = true;
    private boolean isShowProgress = false;
    private String infoMsg;

    private String TAG = getClass().getSimpleName();

    protected CommonSubscriber(Context context) {
        this.mContext = context;
    }

    protected CommonSubscriber(IView view) {
        //this(view,true);
        this.mView = view;
    }


    protected CommonSubscriber(IView view, String errorMsg) {
        this.mView = view;
        this.mErrorMsg = errorMsg;
    }

    protected CommonSubscriber(IView view, boolean isShowProgress) {
        this.mView = view;
        this.isShowProgress = isShowProgress;
    }



    protected CommonSubscriber(IView view, boolean isShowProgress,String infoMsg) {
        this.mView = view;
        this.isShowProgress = isShowProgress;
        this.infoMsg=infoMsg;
    }

    protected CommonSubscriber(IView view, String errorMsg, boolean isShowErrorState) {
        this.mView = view;
        this.mErrorMsg = errorMsg;
        this.isShowErrorState = isShowErrorState;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mView != null && isShowProgress) {
            mView.showLoading(infoMsg);
        }

    }

    @Override
    public void onNext(T t) {
        if (mView != null && isShowProgress) {
            mView.hideLoading();
        }
    }

    @Override
    public void onComplete() {
        Log.d(TAG, "onComplete");
        if (mView!=null&&isShowProgress){
            mView.hideLoading();
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        if (mView == null && mContext == null) {
            return;
        }
        if (mView != null && isShowProgress) {
            mView.hideLoading();
        }
        showError(e);
    }

    private void showError(Throwable e) {
        if (mView != null) {
            if (mErrorMsg != null && !TextUtils.isEmpty(mErrorMsg)) {
                mView.showError(mErrorMsg);
            } else if (e instanceof ApiException) {
                switch (((ApiException) e).getErrorCode()) {
                    case "2000":
                        mView.showReloginDialog(R.string.account_pls_login);
                        break;
                    case "2016":
                        mView.showReloginDialog(R.string.account_locked);
                        break;
                    case "2017":
                        mView.showReloginDialog(R.string.account_locked_else);
                        break;
                    case "8000":
                        mView.showReloginDialog(R.string.account_other_login);
                        break;
                    default:
                        mView.showError(e.getMessage());
                }

            } else if (e instanceof HttpException | e instanceof SocketTimeoutException | e instanceof UnknownHostException) {
                mView.showError(R.string.msg_error_network);
            } else {
                mView.showError(R.string.msg_error_unknown);
            }
        } else if (mContext != null) {
            if (mErrorMsg != null && !TextUtils.isEmpty(mErrorMsg)) {
                Toasty.error(mContext, mErrorMsg, Toast.LENGTH_SHORT).show();
            } else if (e instanceof ApiException) {
                Toasty.error(mContext, e.toString(), Toast.LENGTH_SHORT).show();
            } else if (e instanceof HttpException) {
                Toasty.error(
                        mContext,
                        mContext.getString(R.string.msg_error_network),
                        Toast.LENGTH_SHORT
                ).show();
            }
        }
    }

}
